package com.hsb.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsb.seckill.dao.UserDao;
import com.hsb.seckill.entity.User;
import com.hsb.seckill.exception.GlobalException;
import com.hsb.seckill.service.UserService;
import com.hsb.seckill.utils.CookieUtil;
import com.hsb.seckill.utils.MD5Util;
import com.hsb.seckill.utils.UUIDUtil;
import com.hsb.seckill.vo.LoginVo;
import com.hsb.seckill.result.ResBean;
import com.hsb.seckill.result.ResBeanEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.plugin.util.UIUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2021-09-04 15:57:33
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao,User> implements UserService {
    @Resource
    private UserDao userDao;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param loginVo
     * @param request
     * @param response
     * @return
     */
    @Override
    public ResBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
//        //判断手机号码或密码是否为空
//        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
//            return ResBean.error(ResBeanEnum.LOGIN_ERROR);
//        }
//        //校验手机号码是否合法
//        if (!ValidatorUtil.isMobile(mobile)){
//            return ResBean.error(ResBeanEnum.MOBILE_ERROR);
//        }
        //根据手机号查询用户
        User user = userDao.queryByMobile(mobile);
        //判断用户是否存在
        if (null == user){
            //return ResBean.error(ResBeanEnum.LOGIN_ERROR);
            throw new GlobalException(ResBeanEnum.LOGIN_ERROR);
        }
        //判断密码是否正确
        if (!MD5Util.fromPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            //return ResBean.error(ResBeanEnum.LOGIN_ERROR);
            throw new GlobalException(ResBeanEnum.LOGIN_ERROR);
        }

        //设置session和cookie
        String ticket = UUIDUtil.uuid();
//        request.getSession().setAttribute(ticket,user);
        //将用户信息存入redis中
        //redisTemplate.opsForValue().set("user:"+ticket,user,30, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set("user:"+ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);

        return ResBean.success(ticket);
    }

    /**
     * 根据cookie获取用户
     * @param userTicket
     * @return
     */
    @Override
    public User getUserByCookie(String userTicket,HttpServletRequest request,HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        if (user != null){
            CookieUtil.setCookie(request,response,"userTicket",userTicket);
        }
        return user;
    }

    /**
     * 更新用户密码
     * @param ticket
     * @param password
     * @return
     */
    @Override
    public ResBean updatePassword(String ticket, String password,HttpServletRequest request,HttpServletResponse response) {
        User user = getUserByCookie(ticket, request, response);
        if (user == null){
            throw new GlobalException(ResBeanEnum.MOBILE_NOT_EXIST);
        }
        user.setPassword(MD5Util.inputPassToDBPass(password,user.getSlat()));
        int result = userDao.updateById(user);
        if (1 == result){
            // 删除redis中的user缓存
            redisTemplate.delete("user:"+ticket);
            return ResBean.success();
        }
        return ResBean.error(ResBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}